(function () {
    'use strict';

    angular
        .module('app.ForgotPassword')
        .controller('ForgotPasswordController', ForgotPasswordController);

    ForgotPasswordController.$inject = ['logger','$state','$rootScope','UserContextData','$mdDialog','$timeout'];
    /* @ngInject */
    function ForgotPasswordController(logger, $state, $rootScope, UserContextData,$mdDialog,$timeout) {
        var vm = this;
        vm.title = 'ForgotPassword';
        vm.user = "";
        vm.password = "";
        vm.ErrorMessage = "";
        vm.CancelClick = CancelClick;
        vm.ResetClick = ResetClick;
        vm.showqn = false;
        activate();
        
        function activate() {
        }

        function CancelClick() {
            $mdDialog.hide();
        };
        
        function ResetClick() {
            //$mdDialog.hide();
            //vm.showqn = true;
            //alert(vm.showqn);
            if (vm.showqn == false) {
                var promise = UserContextData.GetUserQuestion(vm.user).then(function (response) {
                    if (response.data.SecretQuestion) {
                        vm.secretqn = response.data.SecretQuestion;
                        vm.ErrorMessage = response.data.ErrorMsg;
                        vm.showqn = true;
                    }
                });
                            }
            if (vm.showqn == true) {
                var promise1 = UserContextData.UpdateUserPassword(vm.user, vm.secretans, vm.password).then(function (response) {
                    if (response.data.ErrorMsg) {
                        vm.ErrorMessage = response.data.ErrorMsg;
                        if(response.data.ErrorMsg =="Password Updated")
                        {
                            $mdDialog.show(
                          $mdDialog.alert()
                            .parent(angular.element(document.body))
                            .clickOutsideToClose(true)
                            .title('Reset Password')
                            .textContent(vm.ErrorMessage)
                            .ariaLabel('Reset Password Dialog')
                            .ok('Close')
                        ).then(function (response) {
                            $timeout(function () {
                                $mdDialog.hide();
                            }, 100);
                        });
                        }
                    }
                })
            }
        }

        function showWait() {
            $mdDialog.show({
                controller: 'LoginController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        }
    }
})();


